package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IPurchaseService;

import javax.annotation.Resource;

/**
 * 采购单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-05-06
 */
@Service
public class PurchaseServiceImpl implements IPurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private PurchaseRecordMapper purchaseRecordMapper;
    @Autowired
    private WarehousingMapper warehousingMapper;
    @Resource
    private com.ruoyi.system.mapper.WatehouseMaterialMapper WatehouseMaterialMapper;

    @Resource
    private com.ruoyi.system.mapper.WarehouseDetailMapper WarehouseDetailMapper;

    @Resource
    private MaterialMapper materialMapper;

    @Override
    public Purchase selectPurchaseById(Long id) {
        return purchaseMapper.selectPurchaseById(id);
    }


    @Override
    public List<Purchase> selectPurchaseList(Purchase purchase) {

        List<Purchase> purchases = purchaseMapper.selectPurchaseList(purchase);

        return purchases;
    }


    @Override
    public int insertPurchase(Purchase purchase) {
        return purchaseMapper.insertPurchase(purchase);
    }

    //修改采购单
    @Override
    public int updatePurchase(Purchase purchase) {
        Long id = purchase.getId();
        //插入材料详情表
        double full_amout = 0.0;
        List<PurchaseRecord> purchaseRecords = purchase.getPurchaseRecords();
        for (int i = 0; i < purchaseRecords.size(); i++) {
            System.out.println(purchaseRecords.get(i));
            if (null == purchaseRecords.get(i).getId()) {
                //证明这是个新添材料
                MaterialThree materialThree = new MaterialThree();
                materialThree.setmName(purchaseRecords.get(i).getmName());
                materialThree.setmModel(purchaseRecords.get(i).getmModel());
                materialThree.setmUnit(purchaseRecords.get(i).getmUnit());
                Long mid=materialMapper.selectMId(materialThree);
                //查看材料表是否有这种材料
                if (null==mid) {
                    //给材料表添加材料
                    materialMapper.insertMatOnly(materialThree);
                    purchaseRecords.get(i).setmId(materialThree.getmId());
                }else {
                    purchaseRecords.get(i).setmId(mid);
                }

                purchaseRecords.get(i).setPurId(id);
                purchaseRecordMapper.insertPurchaseRecord(purchaseRecords.get(i));
            } else {
                MaterialThree materialThree = new MaterialThree();
                materialThree.setmName(purchaseRecords.get(i).getmName());
                materialThree.setmModel(purchaseRecords.get(i).getmModel());
                materialThree.setmUnit(purchaseRecords.get(i).getmUnit());
                Long mid=materialMapper.selectMId(materialThree);
                //查看材料表是否有这种材料
                if (null==mid) {
                    //给材料表添加材料
                    materialMapper.insertMatOnly(materialThree);
                    purchaseRecords.get(i).setmId(materialThree.getmId());
                }else {
                    purchaseRecords.get(i).setmId(mid);
                }
                //做修改即可
                purchaseRecordMapper.updatePurchaseRecord(purchaseRecords.get(i));
            }
            full_amout += purchaseRecords.get(i).getMdPrice() * purchaseRecords.get(i).getMdSum();
        }
        purchase.setFullAmount(full_amout);
        purchaseMapper.updatePurchase(purchase);
        return 1;
    }

    /**
     * 批量删除采购单
     *
     * @param ids 需要删除的采购单ID
     * @return 结果
     */
    @Override
    public int deletePurchaseByIds(Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            //修改请购单状态
            purchaseMapper.updateState(ids[i]);
            //删除生成的出入库单
            purchaseMapper.deleteAllWareHouring(ids[i]);
            //删除所有小材料
            purchaseMapper.deleteAllMat(ids[i]);
            //删除采购单
            purchaseMapper.deletePurchaseById(ids[i]);
        }
        return 1;
    }

    /**
     * 删除采购单信息
     *
     * @param id 采购单ID
     * @return 结果
     */
    @Override
    public int deletePurchaseById(Long id) {
        //修改状态
        return purchaseMapper.deletePurchaseById(id);
    }

    //新增采购单
    @Override
    public int insertPurchaseRecord(Purchase purchase) {
        //插入大表
        purchaseMapper.insertPurchase(purchase);
        //插入小表
        Long id = purchase.getId();
        System.out.println(id);
        //插入材料详情表
        double full_amout = 0.0;
        List<PurchaseRecord> purchaseRecords = purchase.getPurchaseRecords();
        for (int i = 0; i < purchaseRecords.size(); i++) {
            PurchaseRecord purchaseRecord = purchaseRecords.get(i);
            //证明这是个新添材料
            MaterialThree materialThree = new MaterialThree();
            materialThree.setmName(purchaseRecords.get(i).getmName());
            materialThree.setmModel(purchaseRecords.get(i).getmModel());
            materialThree.setmUnit(purchaseRecords.get(i).getmUnit());
            Long mid=materialMapper.selectMId(materialThree);
            //查看材料表是否有这种材料
            if (null==mid) {
                //给材料表添加材料
                materialMapper.insertMatOnly(materialThree);
                purchaseRecords.get(i).setmId(materialThree.getmId());
            }else {
                purchaseRecords.get(i).setmId(mid);
            }



            //设置外键
            purchaseRecord.setPurId(id);
            full_amout += purchaseRecord.getMdPrice() * purchaseRecord.getMdSum();
            purchaseRecordMapper.insertPurchaseRecord(purchaseRecord);
        }
        purchase.setFullAmount(full_amout);
        purchaseMapper.updatePurchase(purchase);
        return 1;
    }

    //获取运费
    @Override
    public Double getShipping(long pro_id) {
        return purchaseMapper.getShipping(pro_id);
    }

    //删除单条
    @Override
    public int deleteMat(long id) {
        //如果是请购生成需要修改状态
        purchaseMapper.updateReqState(id);
        return purchaseMapper.deleteMat(id);
    }

    //生成入库单
    @Override
    public int addInMonad(Long purId) {
        Purchase purchase = purchaseMapper.selectPurchaseByPurId(purId);
        //生成一张乙购入库单
        Warehousing warehousing1 = new Warehousing();
        warehousing1.setPurId(purId);
        warehousing1.setType(1);
        warehousing1.setOprType(0);
        warehousingMapper.insertWarehousing(warehousing1);
        //获得新插入的入库单id
        Long warehousing_id = warehousing1.getId();
        //获得采购单Id
        Long rec_pur_id = purchase.getId();
        HashMap<String, Long> map = new HashMap();
        map.put("warehousing_id", warehousing_id);
        map.put("rec_pur_id", rec_pur_id);
        map.put("md_inType", 1L);
        //插入材料
        purchaseMapper.addIntoMat(map);
        purchase.setStateIn(warehousing_id);
        purchaseMapper.updatePurchase(purchase);
        return 1;
    }

    //生成出库单
    @Override
    public int addOutMonad(Long pur_id) {
        //先查看入库单是否生成且已经全部入库
        if (purchaseMapper.selectInstate(pur_id) <= 0) {
            Purchase purchase = purchaseMapper.selectPurchaseByPurId(pur_id);

            Long rec_pur_id = purchase.getId();
            HashMap<String, Long> map = new HashMap();
            map.put("rec_pur_id", rec_pur_id);
            Warehousing warehousing2 = new Warehousing();
            warehousing2.setPurId(pur_id);
            warehousing2.setOprType(1);
            warehousingMapper.insertWarehousing(warehousing2);
            Long outing_id = warehousing2.getId();
            map.put("outing_id", outing_id);
            //插入材料
            purchaseMapper.addOutMat(map);
            purchase.setStateOut(outing_id);
            purchaseMapper.updatePurchase(purchase);
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public Purchase selectPurchaseSum(Long id) {
        Purchase purchase = purchaseMapper.selectPurchaseById(id);
        DecimalFormat df = new DecimalFormat("######0.00");
//        df.setRoundingMode(RoundingMode.HALF_UP);
        Double sum = 0D;
        if (purchase.getPurchaseRecords() != null) {
            for (int i = 0; i < purchase.getPurchaseRecords().size(); i++) {
                Double mdSum = purchase.getPurchaseRecords().get(i).getMdSum();
                Double mdPrice = purchase.getPurchaseRecords().get(i).getMdPrice();
                if (null == mdSum) {
                    mdSum = 0D;
                }
                if (null == mdPrice) {
                    mdPrice = 0D;
                }
//                Double aDouble = Double.valueOf(df.format(mdSum * mdPrice));
                    purchase.getPurchaseRecords().get(i).setSum(new BigDecimal(new Double(mdSum * mdPrice).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                sum += purchase.getPurchaseRecords().get(i).getSum();

            }
            if(purchase.getShipping()!=null){
                sum+=purchase.getShipping();
            }
        }
        purchase.setFullAmount(Double.valueOf(df.format(sum)));
        return purchase;
    }

    @Override
    public int stateOfPurchase(Long pur_id) {
        if (purchaseMapper.stateOfPurchase(pur_id) <= 0) {
            return 1;


        } else {
            return 0;
        }

    }



}
